package tq.com.tqim.socket.message;

import org.json.JSONException;
import org.json.JSONObject;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

public class TQIMSMessage {

        static protected String mFlag = "COFF";
        protected int bodyLen = 0;

        protected String mBody;
        static public int MSG_HEADER_LEN = 8;

        static public TQIMSMessage writeBody(String body){
            TQIMSMessage msg = new TQIMSMessage();
            msg.mBody = body;
            msg.bodyLen = body.length();
            return msg;
        }

        static public TQIMSMessage readHeader(ByteBuffer buffer){
            if(buffer == null || buffer.limit() < MSG_HEADER_LEN){
                return null;
            }
            byte[] flag = new byte[4];
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            buffer.get(flag);
            String strFlag = new String(flag);
            if(!strFlag.equals(mFlag)){
                return null;
            }

            int bodyLen = buffer.getInt();
            return new TQIMSMessage(bodyLen);
        }

        public TQIMSMessage(){

        }

        public TQIMSMessage(int bodyLen){
            this.bodyLen = bodyLen;
        }
        public ByteBuffer getBuffer(){
            ByteBuffer buffer = ByteBuffer.allocate(MSG_HEADER_LEN + bodyLen);
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            buffer.put(mFlag.getBytes());
            buffer.putInt(bodyLen);
            buffer.put(mBody.getBytes());
            buffer.flip();
            return buffer;
        }

        public int getBodyLen(){return bodyLen;}
        public void setBody(String body){
            mBody = body;
            bodyLen = body.length();
        }
        public void setBody(ByteBuffer bodyBuffer){
            Charset charset = Charset.forName("UTF-8");
            mBody = charset.decode(bodyBuffer).toString();
            bodyLen = mBody.length();
        }

        public boolean hasHeader(){
            return bodyLen > 0;
        }
        public boolean hasBody(){
            return (mBody != null && mBody.length() > 0);
        }

        public JSONObject bodyJson(){
            if(mBody == null){
                return null;
            }

            try {
                JSONObject object = new JSONObject(mBody);

                return object;
            }catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }

        public boolean checkHeader(ByteBuffer buffer){
            if(buffer == null || (buffer.limit() - buffer.position()) < MSG_HEADER_LEN){
                return false;
            }
            byte[] flag = new byte[4];
            buffer.order(ByteOrder.LITTLE_ENDIAN);
            buffer.get(flag);
            String strFlag = new String(flag);
            if(!strFlag.equals(mFlag)){
                return false;
            }

            int bodyLen = buffer.getInt();
            this.bodyLen = bodyLen;
            return bodyLen > 0;
        }
}
